package com.apinji.unionpay;

import com.apinji.common.domain.wrapper.Reply;
import com.apinji.common.plugin.Mutual;
import com.apinji.common.plugin.StrUtils;
import com.apinji.unionpay.dto.RefundDto;
import com.apinji.unionpay.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;

/**
 * Created by 轩辕夏夏 on 2017/7/4.
 *
 * 银联支付
 */
@RestController
@EnableAutoConfiguration
@EnableAsync
@RequestMapping("/v1.0/refund")
public class RefundController {

    @Autowired
    private RefundService refundService;


    /**
     *
     * @param request
     * @param refundDto
     * @return
     */
    @RequestMapping(value = "/refund")
    public Reply refund(HttpServletRequest request, @RequestBody RefundDto refundDto){
        if(StrUtils.isEmpty(refundDto.getUserUUID())) {
            return Mutual.invalidParamAppend("userUUID");
        }
        if(StrUtils.isEmpty(refundDto.getRefundOutOderId())) {
            return Mutual.invalidParamAppend("refundOutOderId");
        }
        if(StrUtils.isEmpty((refundDto.getConsumeOutOrderId()))) {
            return Mutual.invalidParamAppend("consumeOutOrderId");
        }
        if(refundDto.getTxnAmt() <=0) {
            return Mutual.invalidParam("退款金额错误");
        }
        if(!Pattern.matches("^[0-9a-zA-Z]{4,40}", refundDto.getRefundOutOderId())) {
            return Mutual.invalidParam("[refundOutOderId]退款账单流水ID错误");
        }
        return refundService.refundOpt(request, refundDto);
    }


    @RequestMapping(value = "/backRcvResponse")
    public void callBackBackRcvResponse(HttpServletRequest request, HttpServletResponse response) throws Exception {
        refundService.backRcvResponse(request, response);
    }



}
